﻿using Accouting804.Setting;
using Accouting804.Views;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Accouting804.ViewModels.AccountViewModels
{
    public class QQ
    {
        public string Icon { get; set; }
        public string Title { get; set; }
    }
    public class AccountItemsViewModel : AccountsBaseViewModel
    {

        public ObservableCollection<AccountRecord> Items { get; set; } = new ObservableCollection<AccountRecord>();

        public IQueryFilterOfAccountRecords queryFilterOfAccountRecords { get; set; }

        public QQ QQ { get; set; } = new QQ() { Title = "test", Icon = "asdfasd" };

        public Command LoadItemsCommand { get; set; }

        public Expression<Func<AccountRecord, bool>> ExpressionWhere { get; set; }

        public AccountItemsViewModel()
        {
            Title = "记账记录";

            LoadItemsCommand = new Command(async () =>
            {
                await ExecuteLoadItemsCommand();

            });


            MessagingCenter.Subscribe<NewAccountItemPage, AccountRecord>(this, "AddAccountItem", async (obj, item) =>
            {
                var newItem = item as AccountRecord;
                Items.Add(newItem);
                await AccountDataStore.AddItemAsync(newItem);
            });


        }

        protected async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy) return;
            IsBusy = true;
            try
            {
                Items.Clear();



                //var items = await AccountDataStore.GetItemsAsync(true);

                var items = await AccountDataStore.GetItemsFilterQueryAsync(forceRefresh: true);

                //items = await DataStore.GetItemsFilterQueryAsync(true);
                foreach (var item in items)
                {
                    Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
            finally
            {
                IsBusy = false;
            }
        }

        protected virtual string Name { get; set; }







    }

}
